package gemini.castor.ui.client.page.content.home.register.distributorid;

import gemini.basic.exception.UserExistedException;
import gemini.basic.model.Distributor;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.home.register.RegisterEvent;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class DistributorIDPresenter extends
		CastorWidgetPresenter<DistributorIDPresenter.Display> {

	@Inject
	public DistributorIDPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}
	
	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getResetClickHandlers();
		HasClickHandlers getSubmitClickHandlers();
		boolean isValidate();
		DistributorIDObject getObject();
	}
	
	private Object outputObject;

	public static final Place PLACE = new Place(PlaceConstants.PLACE_REGISTER);

	@Override
	protected void onBind() {

		registerHandler(display.getSubmitClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						if (display.isValidate()) {
							checkDistributorInfo();
						}
					}
				}));
		
		display.loadData();
	}
	
	private void  checkDistributorInfo(){
		final String code = display.getObject().getDistributorCode();
		String pin = display.getObject().getDistributorPIN();

		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getDistributorService().getDistributorByCodePin( code, pin,
				new AbstractAsyncCallback<Distributor>() {

					@Override
					public void onSuccess(Distributor result) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
						if (result != null) {
							forwardIfSuccess(result);
						} else {
							GuiUtils.alert("Cannot find distributor with code: <"
									+ code
									+ "><br />Or pin is not correct!"
									+ "<br />Please use Forgot Pin service if needed!");
						}
					}
					
					@Override
				    public void onFailure(final Throwable caught) {
						if(caught  instanceof UserExistedException) {
							GuiUtils.alert("This distributor code is had user account already!");
						} else {
							super.onFailure(caught);
						}
					}
				});
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	@Override
	public Object getOutputObject() {
		return outputObject;
	}
	
	private void forwardIfSuccess(Distributor distributor){
		outputObject = distributor;
		localEventBus.fireEvent(new RegisterEvent(
				RegisterEvent.ForwardType.SUBMIT));
	}
}
